package com.shop.loan.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.LoanOrderSignLogByMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.entity.*;
import com.shop.loan.model.enums.*;
import com.shop.loan.model.vo.RentalDataBackVo;
import com.shop.loan.oss.UCloudUtil;
import com.shop.loan.rentaldata.RentalDataUtil;
import com.shop.loan.service.EruptUserDataService;
import com.shop.loan.service.MsgService;
import com.shop.loan.service.NotifyService;
import com.shop.loan.service.SettingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

@Service
public class NotifyServiceImpl implements NotifyService {

    @Resource
    LoanOrderSignLogByMybatisMapper loanOrderSignLogByMybatisMapper;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    RentalDataUtil rentalDataUtil;

    @Resource
    UCloudUtil uCloudUtil;

    @Resource
    SettingService settingService;

    @Resource
    EruptUserDataService eruptUserDataService;

    @Value("${sys.file.path}")
    private String sysFilePath;

    @Resource
    MsgService msgService;

    @Override
    public ResResult rentalDataBack(RentalDataBackVo rentalDataBackVo) {
        if (rentalDataBackVo.getAction() == null) {
            return new ResResult(Code.PARAM_INVALID);
        }

        if (rentalDataBackVo.getAction().intValue() == RentalDataActionType.sign.getType()) {
            if (!StringUtils.hasText(rentalDataBackVo.getContractNo()) || rentalDataBackVo.getStatus() == null) {
                return new ResResult(Code.PARAM_INVALID);
            }
            LoanOrderSignLogByMybatis loanOrderSignLogByMybatis = loanOrderSignLogByMybatisMapper.selectByContractNo(rentalDataBackVo.getContractNo());
            if (loanOrderSignLogByMybatis == null || loanOrderSignLogByMybatis.getId() == null) {
                return new ResResult(Code.PARAM_INVALID);
            }
            /**
             * 下载合同文件操作
             */
            ResResult contractRes = rentalDataUtil.downloadContract(rentalDataBackVo.getContractNo());
            if (contractRes.getCode().intValue() == Code.SUCCESS.getCode()) {
                String signFileUrl = null;
                JSONArray dataArray = (JSONArray) contractRes.getData();
                for (int i = 0; i < dataArray.size(); i++) {
                    String localPath = sysFilePath + "/contract/sign/sign_" + rentalDataBackVo.getContractNo() + "_" + i + ".pdf";
                    HttpUtil.downloadFile(dataArray.get(i).toString(), localPath);
                    if (StringUtils.hasText(signFileUrl)) {
                        signFileUrl = signFileUrl + "|" + uCloudUtil.putFile(localPath, null);
                    } else {
                        signFileUrl = "" + uCloudUtil.putFile(localPath, null);
                    }
                }
                loanOrderSignLogByMybatis.setSignFileUrl(signFileUrl);
            }
            loanOrderSignLogByMybatis.setUserSignStatus(rentalDataBackVo.getStatus());
            loanOrderSignLogByMybatisMapper.updateByPrimaryKeySelective(loanOrderSignLogByMybatis);

            //修改订单签署状态
            LoanOrderByMybatis loanOrderByMybatis = new LoanOrderByMybatis();
            loanOrderByMybatis.setId(loanOrderSignLogByMybatis.getOrderId());
            loanOrderByMybatis.setSignStatus(rentalDataBackVo.getStatus());
            loanOrderByMybatisMapper.updateByPrimaryKeySelective(loanOrderByMybatis);
        }

        return new ResResult(Code.SUCCESS);
    }


}
